<!--
 * new page
 * @author: liuxin
 * @since: 2024-08-16
 * delete_admin.vue
-->
<template>
	<el-dialog v-model="dialogFormVisible" :title='title' width="30%" center>
		<span>{{ tips.value }}</span>
		<el-form-item :label="tips" class="fold_label" label-width="100%" />
		<!-- <span v-if="title.value  == '删除信息'" v-html="tips"></span>
		<span v-else-if="title.value == '恢复消息'" v-html="tips"></span>
		<span v-else="title.value == '真正删除信息'" v-html="tips"></span>  -->
		<template #footer>
			<span class="dialog-footer">
				<el-button type="primary" @click="operationMessageFn">确定</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { ref, onBeforeUnmount } from "vue"
import { bus } from "@/utils/mitt.js"
import { firstDelete, recover, delMessage } from '@/api/message'
import { ElMessage } from 'element-plus'
import { changeUserReadListButDelete, changeUserReadList } from "@/api/dep_msg.js"
import { useMsg } from "@/store/message"

const msgStore = useMsg()
const title = ref()
const tips = ref()
// 消息ID
const messageid = ref()
const department = ref()
bus.on('deleteMessageId', (row: any) => {
	title.value = '删除信息'
	tips.value = '您确定要删除这个公告吗？'
	messageid.value = row.id
	department.value = row.message_receipt_object
})
bus.on('renewID', (row: any) => {
	title.value = '恢复消息'
	tips.value = '您确定要恢复这个公告吗？'
	messageid.value = row.id
	department.value = row.message_receipt_object
})
bus.on('realDelteID', (id: number) => {
	title.value = '真正删除信息'
	tips.value = '请谨慎操作，您确定要真正删除这个公告吗？'
	messageid.value = id
})
const emit = defineEmits(['success'])
// 删除产品
const operationMessageFn = async () => {
	if (title.value == '删除信息') {
		const res = await firstDelete(messageid.value)
		if (res.status == 0) {
			await changeUserReadListButDelete(messageid.value, department.value)
			msgStore.returnReadList(localStorage.getItem('id'))
			ElMessage({
				message: '删除公告成功',
				type: 'success'
			})
			emit('success')
			dialogFormVisible.value = false
		} else {
			ElMessage.error('删除公告失败')
			dialogFormVisible.value = false
		}
	}
	if (title.value == '恢复消息') {
		const res = await recover(messageid.value)
		if (res.status == 0) {
			await changeUserReadList(messageid.value, department.value)
			msgStore.returnReadList(localStorage.getItem('id'))
			console.log(messageid.value, department.value);
			ElMessage({
				message: '恢复公告成功',
				type: 'success'
			})
			emit('success')
			dialogFormVisible.value = false
		} else {
			ElMessage.error('恢复公告失败')
			dialogFormVisible.value = false
		}
	}
	if (title.value == '真正删除信息') {
		const res = await delMessage(messageid.value)
		if (res.status == 0) {
			ElMessage({
				message: '删除公告成功',
				type: 'success'
			})
			emit('success')
			dialogFormVisible.value = false
		} else {
			ElMessage.error('删除公告失败')
			dialogFormVisible.value = false
		}
	}
}


// 弹窗开关

const dialogFormVisible = ref(false)
// 打开编辑管理员弹窗
const open = () => {
	dialogFormVisible.value = true
}
defineExpose({
	open
})

// 取消订阅/监听
onBeforeUnmount(() => {
	bus.all.clear()
})
</script>

<style scoped lang="scss">
.fold_label .el-form-item__label {
	white-space: pre-line;
	text-align-last: left; //最后一行两端对齐
	text-align: center;
	justify-content: space-between;
	margin-top: -4px;
	line-height: 25px;
	text-justify: distribute-all-lines;
}

:deep(.el-form-item__label) {
	text-align: center;
	float: none;
	word-break: break-word;
	justify-content: space-between;
}
</style>